﻿using System;
using System.Collections.Generic;
using Relocation.Service;
using System.Linq;
using System.Linq.Expressions;

namespace Relocation.Data
{
	/// <summary>
	///	拆迁户
	/// </summary>
	public partial class Relocatee
	{
		/// <summary>
		/// 安置补助合计.
		/// </summary>
		/// <value>The total.</value>
		[global::System.Runtime.Serialization.DataMemberAttribute()]
		public global::System.Nullable<decimal> total
		{
			get
			{
				return this.money1 + this.money2 + this.money3;
			}
		}

		/// <summary>
		/// 搜索时是否将status纳入搜索条件，默认为是（True）
		/// </summary>
		public bool IsSearchStatus = true;

		/// <summary>
		/// 需要安置的人数
		/// </summary>
		public int PlacementCount
		{
			get
			{
				return this.Placements.Count;
			}
		}

		/// <summary>
		/// 已经安置的人数
		/// </summary>
		public decimal AlreadyCount
		{
			get
			{
				//if(!this.Relocatees_House.IsLoaded)
				//{
				//      this.Relocatees_House.Load();
				//}				
				return this.Relocatees_House.Sum(t => t.persons.Value);
			}
		}

		public Relocatee()
			: base()
		{
			Dictionary<Type, int> _primaryKeysCache = DataModel.primaryKeysCache;
			lock (_primaryKeysCache)
			{
				int primaryKey = 0;
				if (!_primaryKeysCache.TryGetValue(this.GetType(), out primaryKey))
				{
					using (DataModel model = new DataModel())
					{
						primaryKey = model.Relocatees.Any() ? model.Relocatees.Max(c => c.relocatees_id) : 0;
					}
				}
				_primaryKeysCache[this.GetType()] = primaryKey;
			}
		}

		/// <summary>
		/// 信息没有录入完整的标记
		/// </summary>
		public bool IsNotCompleted
		{
			get
			{
				return (string.IsNullOrEmpty(this.head)
					|| string.IsNullOrEmpty(this.organization)
					|| string.IsNullOrEmpty(this.village)
					|| IsNullOrZero(this.area)
					|| string.IsNullOrEmpty(this.areaType)
					|| IsNullOrZero(this.money1)
					|| IsNullOrZero(this.money2)
					|| this.enabledate == null
					|| string.IsNullOrEmpty(this.principal)
					|| IsNullOrZero(this.payment)
					|| (IsNullOrZero(this.persons) &&
						IsNullOrZero(this.housetype1) &&
						IsNullOrZero(this.housetype2) &&
						IsNullOrZero(this.housetype3) &&
						IsNullOrZero(this.housetype4) &&
						IsNullOrZero(this.housetype5) &&
						IsNullOrZero(this.housetype6) &&
						IsNullOrZero(this.housetype7) &&
						IsNullOrZero(this.housetype1))

					);

			}
		}

		static public bool IsNullOrZero(int? value)
		{
			return value.HasValue ? value == 0 : true;
		}

		static public bool IsNullOrZero(decimal? value)
		{
			return value.HasValue ? value == 0 : true;
		}

		static public string GetPropName(Expression<Func<Relocatee, object>> expr)
		{
			switch (expr.Body.NodeType)
			{
				case ExpressionType.MemberAccess:
					return ((MemberExpression)expr.Body).Member.Name;
				case ExpressionType.Convert:
					return ((MemberExpression)((UnaryExpression)expr.Body).Operand).Member.Name;
				default:
					return null;
			}
		}

		/// <summary>
		/// 更新拆迁户的状态信息
		/// </summary>
		/// <param name="relocate"></param>
		public void UpdateStatus()
		{
			try
			{
				//已安置的个数	
				if (!this.Relocatees_House.IsLoaded)
					this.Relocatees_House.Load();
				int count = this.Relocatees_House.Count;

				//意向安置的个数
				int count2 = (this.housetype1 == null ? 0 : (int)this.housetype1) +
					(this.housetype2 == null ? 0 : (int)this.housetype2) +
					(this.housetype3 == null ? 0 : (int)this.housetype3) +
					(this.housetype4 == null ? 0 : (int)this.housetype4) +
					(this.housetype5 == null ? 0 : (int)this.housetype5) +
					(this.housetype6 == null ? 0 : (int)this.housetype6) +
					(this.housetype7 == null ? 0 : (int)this.housetype7);

				if (count2 == 0)
				{
					this.status = 2;
				} else
				{
					if (count == 0)
					{
						this.status = 0;
					} else
					{
						if (count2 <= count)
						{
							this.status = 2;
						} else
						{
							this.status = 1;
						}
					}
				}
			} catch (Exception ex)
			{
				Console.WriteLine(ex);
				throw;
			}
		}


		/// <summary>
		/// 返回已安置的人口数量
		/// </summary>
		/// <returns></returns>
		public decimal GetPlacedPeopleCount()
		{
			return this.Relocatees_House.CreateSourceQuery().Sum(it => it.persons).GetValueOrDefault(0);
		}

		/// <summary>
		/// 返回未安置置的人口数量
		/// </summary>
		/// <returns></returns>
		public decimal GetNotPlacedPeopleCount()
		{
			return this.persons.GetValueOrDefault(0) - this.GetPlacedPeopleCount();
		}



		//void Onrelocatees_house_idChanging(global::System.Int32 value)
		//{ 

		//}

	}
}
